package the.one.weather.util;

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import the.one.weather.model.City;
import the.one.weather.model.Weather;
import the.one.weather.model.WeatherFuture;
import the.one.weather.model.WeatherTips;

/**
 * @author The one
 * @date 2019/5/10 0010
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
public class LitePalUtil {

    public static final String weather_id = "weather_id = ?";
    public static final String cityWeather = "currentCity = ?";
    public static final String lbs = "lbs = ?";

    public static final String isLbs = "isLbs";
    public static final String noLbs = "noLbs";

    public static final String order = "addTime asc";

    /**
     * 获取定位到的天气城市列表 只有一个
     * @return
     */
    public static Weather getLbsWeatherCity(){
        return LitePal.where(lbs,isLbs).findFirst(Weather.class,true);
    }

    /**
     * 获取普通天气城市列表
     * @return
     */
    public static List<Weather> getNormalWeatherCities(){
        return LitePal.where(lbs,noLbs).order(order).find(Weather.class,true);
    }

    public static List<Weather> getAllWeatherCities(){
        List<Weather> weathers = new ArrayList<>();
        Weather lbs = getLbsWeatherCity();
        if(null != lbs) weathers.add(lbs);
        List<Weather> normals = getNormalWeatherCities();
        if(null != normals &&normals.size()>0) weathers.addAll(normals);
        return weathers;
    }

    public static boolean saveLbsCityWeather(String city){
        return saveWeather(city,isLbs);
    }

    public static boolean saveNormalCityWeather(String city){
        return saveWeather(city,noLbs);
    }

    /**
     * 保存定位到的天气城市
     * @param city
     * @return
     */
    public static boolean saveWeather(String city,String isLbs){
        Weather weather = new Weather();
        weather.setLbs(isLbs);
        weather.setCurrentCity(city);
        weather.setAddTime(System.currentTimeMillis());
        return weather.saveOrUpdate(cityWeather,city);
    }

    /**
     * 删除定位天气城市
     */
    public static void deleteLbsWeatherCity(){
         LitePal.deleteAll(Weather.class, LitePalUtil.lbs, LitePalUtil.isLbs);
    }


    /**
     * 删除当前城市以前的 提示
     * @param weather
     */
    public static void deleteWeatherTips(Weather weather){
        LitePal.deleteAll(WeatherTips.class,weather_id,weather.getId()+"");
    }

    /**
     * 获取当前城市的 提示
     * @param weather
     */
    public static List<WeatherTips> getWeatherTips(Weather weather){
       return LitePal.where(weather_id,weather.getId()+"").find(WeatherTips.class);
    }

    /**
     * 删除当前城市以前的 今后天气数据
     * @param weather
     */
    public static void deleteWeatherFutures(Weather weather){
        LitePal.deleteAll(WeatherFuture.class,weather_id,weather.getId()+"");
    }

    /**
     * 获取当前城市的 今后天气数据
     * @param weather
     */
    public static List<WeatherFuture> getWeatherFutures(Weather weather){
        return LitePal.where(weather_id,weather.getId()+"").find(WeatherFuture.class);
    }


    /**
     * 获取本地保存的城市天气数据
     * @param city
     * @return
     */
    public static Weather getDataBaseCityWeather(String city){
        return LitePal.where(cityWeather,city).findFirst(Weather.class,true);
    }

    public static List<City> getSearchCities(String search){
        if(TextUtils.isEmpty(search)) return null;
       return LitePal.where("name like ?", "%" + search + "%").find(City.class);
    }

    /**
     * 保存当前城市的天气数据
     * @param weather
     */
    public static void saveCityWeather(Weather weather){
        if(null == weather || TextUtils.isEmpty(weather.getCurrentCity())) return;
//        LitePal.deleteAll(WeatherTips.class,"",weather.getCurrentCity());
//        LitePal.deleteAll(Weather.class,cityWeather,weather.getCurrentCity());
        LitePal.saveAll(weather.getTips());
        LitePal.saveAll(weather.getFutures());
        weather.save();
    }

    /**
     * 将天气城市数据写入到数据库
     * @param context
     */
    public static void writeCityData(Context context){
        List<City> cities = LitePal.findAll(City.class);
        if (null == cities || cities.size() == 0) {
            String fileName = "city_v3.json";
            String cityJson = FileUtil.getJson(fileName, context);
            //转换为对象
            cities = new Gson().fromJson(cityJson, new TypeToken<List<City>>() {
            }.getType());
            // 写入到数据库
            LitePal.saveAll(cities);
        }
    }

}
